﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatchParseRegexContent
    {
        private readonly KeRui.DAL.CmsCatchParseRegexContent dal = new KeRui.DAL.CmsCatchParseRegexContent();

        public int Add(KeRui.Model.CmsCatchParseRegexContent model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchParseRegexContent> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchParseRegexContent> list = new List<KeRui.Model.CmsCatchParseRegexContent>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchParseRegexContent item = new KeRui.Model.CmsCatchParseRegexContent();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["ParseContentID"].ToString() != "")
                    {
                        item.ParseContentID = int.Parse(dt.Rows[i]["ParseContentID"].ToString());
                    }
                    item.FieldName = dt.Rows[i]["FieldName"].ToString();
                    item.Pattern = dt.Rows[i]["Pattern"].ToString();
                    item.Replacement = dt.Rows[i]["Replacement"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public bool Exists(int ID)
        {
            return this.dal.Exists(ID);
        }

        public List<KeRui.Model.CmsCatchParseRegexContent> GetList(int ParseContentID)
        {
            string strWhere = "ParseContentID=" + ParseContentID;
            string strOrder = "FieldName desc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseRegexContent", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseRegexContent> GetList(string strCondition)
        {
            string strWhere = strCondition;
            string strOrder = "ID";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseRegexContent", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseRegexContent> GetList(string where, string order, int size, int index)
        {
            string strWhere = where;
            string strOrder = order;
            int pageSize = size;
            int pageIndex = index;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchParseRegexContent", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchParseRegexContent> GetListByCache(string strCondition)
        {
            string cacheKey = "CmsCatchParseRegexContentList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(strCondition);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatchParseRegexContent>) cache;
        }

        public KeRui.Model.CmsCatchParseRegexContent GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.CmsCatchParseRegexContent GetModelByCache(int ID)
        {
            string cacheKey = "CmsCatchParseRegexContentModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchParseRegexContent) cache;
        }

        public void Update(KeRui.Model.CmsCatchParseRegexContent model)
        {
            this.dal.Update(model);
        }
    }
}

